package interfaces;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import ctrl.Controller;




public class PlayScreen extends JPanel implements ActionListener //class to display playing screen
{
	private MainWindow mainWin;
	private JLabel QuestionFrame;
	private AnswerButton[] choicesButton;
	private JButton quitButton;
	private JLabel QuestionMarkLabel;
	private int QuestionMarkLabel_X;
	private int QuestionMarkLabel_Y;
	private int QuestionMarkLabel_W;
	private int QuestionMarkLabel_H;
	private int quitButton_X;
	private int quitButton_Y;
	private String tmpAnswer;
	private boolean isEnd=false;
	private Controller c;
	public PlayScreen(MainWindow win)
	{
		super(); 
		try
		{
			c=new Controller();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mainWin=win;
		this.setLayout(null);
		this.setBackground(Color.white);

		int w=win.getWidth();
		int h=win.getHeight();
		int padding_x=20;
		int padding_y=20;
		w=w-2*padding_x;
		h=h-2*padding_y;
		int QuestionFrame_X;
		int QuestionFrame_Y;
		int QuestionFrame_W;
		int QuestionFrame_H;
		int ChoiceButton_W;
		int ChoiceButton_H;

		//calculate position of GUI components
		QuestionFrame_X=0+padding_x;
		QuestionFrame_Y=3*h/10+padding_y;
		QuestionFrame_W=w/6*5;
		QuestionFrame_H=h/10*2;
		QuestionMarkLabel_X=QuestionFrame_X+QuestionFrame_W;
		QuestionMarkLabel_Y=QuestionFrame_Y-20;
		QuestionMarkLabel_W=QuestionFrame_H;
		QuestionMarkLabel_H=QuestionFrame_H;
		ChoiceButton_W=w/3;
		ChoiceButton_H=h/10;
		quitButton_X=w-13;
		quitButton_Y=0;

		//create GUI components and add into specified position
		QuestionFrame=new JLabel("");
		QuestionFrame.setBorder(new TitledBorder(new LineBorder (Color.black, 1),
		"Question"));
		QuestionFrame.setFont(new Font("Serif", Font.BOLD, 22));
		QuestionFrame.setBounds(QuestionFrame_X,QuestionFrame_Y,QuestionFrame_W,QuestionFrame_H);
		QuestionFrame.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(QuestionFrame);
		QuestionMarkLabel=new JLabel("<html><body color=#00ff00>" +
				"??" +
		"</body></html>");
		QuestionMarkLabel.setFont(new Font("comic sans ms" +
				"", Font.BOLD, 38));
		QuestionMarkLabel.setBounds(QuestionMarkLabel_X,QuestionMarkLabel_Y,QuestionMarkLabel_W,QuestionMarkLabel_H);
		this.add(QuestionMarkLabel);

		ImageIcon quitIcon=new ImageIcon("images/RedClose.gif");
		quitButton=new JButton(quitIcon);
		quitButton.setBounds(quitButton_X,quitButton_Y,quitIcon.getIconWidth(),quitIcon.getIconHeight());
		this.add(quitButton);
		quitButton.addActionListener(this);

		choicesButton=new AnswerButton[2];
		for(int i=0;i<2;i++)
		{
			choicesButton[i]=new AnswerButton();
			choicesButton[i].setBounds(i%2*(ChoiceButton_W+w/9)+padding_x,h/10*6+(i/2)*h/5+padding_y ,ChoiceButton_W,ChoiceButton_H);
			choicesButton[i].addActionListener(this);
			this.add(choicesButton[i]);
		}
		choicesButton[0].setText("YES");	
		choicesButton[1].setText("NO");
		showQuiz();
	}
	private void showQuiz() //show next quiz with question and choices
	{
		String q=c.getQuestion();
		QuestionFrame.setText(q);
	
	}
	private void showAnswer()
	{
		QuestionFrame.setText(c.getAnswer());
		isEnd=true;
	}
	private void quit()//quit game session
	{
		//controller.saveInfo();
		isEnd=false;
		//timer.removeActionListener(uts);
		mainWin.changeScreen(new afterPlayScreen(mainWin));
		return;
	}
	private void endQuiz()//end of a quiz in current session
	{
		int t;
		if(tmpAnswer.equals("YES"))
		{
			t=1;
		}
		else t=0;
		c.setAnswer(t);
		if(c.checkEnd())
		{
			showAnswer();
		}
		else showQuiz();
	}
	private void playSound(String filename) //playing .wav sound file
	{
		try{
			AudioInputStream audioInput = AudioSystem.getAudioInputStream(new File(filename));
			Clip buttonClickSound;
			buttonClickSound = AudioSystem.getClip();
			buttonClickSound.open(audioInput);
			buttonClickSound.start();
		}
		catch (Exception ex){
			ex.printStackTrace();
		}

	}
	@Override
	public void actionPerformed(ActionEvent arg0) //action when an answer button is pressed
	{
		// TODO Auto-generated method stub
		if(arg0.getSource()==quitButton)
		{
			quit();
			return;
		}
		if(isEnd==true)
		{
			quit();
			return;
		}
//		if(waitTime>0)
//		{
//			waitTime--;	
//			return;
//		}
	for(int i=0;i<4;i++)
			if(arg0.getSource()==choicesButton[i])
			{
				tmpAnswer=choicesButton[i].getText();
				endQuiz();
				break;
			}
	}
}
